from django.shortcuts import render,HttpResponse
from django.http import HttpResponse
from django.views.generic import DetailView
from App3.models import Teacher
from pandas import Series, DataFrame
import pandas as pd
# Create your views here.
def index(request):
    if len(Teacher.objects.all()) == 0:
      file0 = pd.read_csv('E:\Project\PythonProject\pythonProject1\Work3\Z3.csv', encoding="gbk")
      df = pd.DataFrame(file0)
      dict = df.to_dict("list")
      for i in range(810):
        list=['','','','','','','','']
        list[0] =dict["name"][i]
        list[1] =dict["gender"][i]
        list[2] =dict["college"][i]
        list[3] =dict["major"][i]
        list[4] =dict["direction"][i]
        list[5] =dict["email"][i]
        list[6] =dict["intro"][i]
        list[7] =dict["photo"][i]
        Teacher.objects.create(name=list[0],gender=list[1],college=list[2],major=list[3],direction=list[4],email=list[5],intro=list[6],photo=list[7])
    ls=Teacher.objects.all()
    #print(ls)
    context={"teachers_list":ls}
    return render(request,"index.html",context)
def list(request):
    ls=Teacher.objects.all()
    context = {"teachers_list": ls}
    return render(request, "list.html", context)

def search(request):
    college =request.POST["dcollege"].strip()
    major=request.POST["dmajor"].strip()
    direction=request.POST["ddirection"].strip()
    name=request.POST["dname"].strip()
    #查询算法设计
    check=[college,major,direction,name]
    checkl=[]
    for i in range(4):
        if check[i]!="":
           checkl.append(i)
    print(checkl)
    if 3 not in checkl:
        if len(checkl) in [0,3]:
            list=Teacher.objects.filter(college__contains=college,major__contains=major,direction__contains=direction)
        elif len(checkl)==2:
            if checkl==[1, 2]:
                list=Teacher.objects.filter(major__contains=major,direction__contains=direction)
            elif checkl==[0, 2]:
                list=Teacher.objects.filter(college__contains=college,direction__contains=direction)
            else :
                list=Teacher.objects.filter(college__contains=college,major__contains=major)
        else:
            if checkl==[2]:
                list=Teacher.objects.filter(direction__contains=direction)
            elif checkl==[0]:
                list=Teacher.objects.filter(college__contains=college)
            else :
                list=Teacher.objects.filter(major__contains=major)
    else:
        if 0 in checkl:
            list=Teacher.objects.filter(college__contains=college,name__contains=name)
        else:
            list=Teacher.objects.filter(name__contains=name)
    context = {"teachers_list": list}
    return render(request, "search.html", context)

class intro(DetailView):
    model=Teacher
    template_name = "intro.html"
    context_object_name = "teacher"
    #pk_url_kwarg = "name"














# def intro(request):
#     #college = request.POST["dcollege"].strip()
#     #major = request.POST["dmajor"].strip()
#     #direction = request.POST["ddirection"].strip()
#     intro=request.POST["dintro"].strip()
#     list=Teacher.objects.filter(intro=intro)
#     print(list)
#     context = {"teacher": list[0]}
#     return render(request, "intro.html", context)




